// 定义防抖函数
const debounce = (func, delay) => {
  let timer = ref(null);
  return function () {
    const context = this;
    const args = arguments;
    if (timer.value) {
      clearTimeout(timer.value);
    }
    timer.value = setTimeout(() => {
      func.apply(context, args);
    }, delay);
  };
};

// 定义自定义指令
export const vDebounce = {
  mounted(el, binding) {
    const delay = binding.arg || 300; // 默认延迟时间为 300 毫秒
    const callback = binding.value;
    el.addEventListener('click', debounce(callback, delay));
  },
};
